package com.beauty.dressing.module.mine.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.beauty.dressing.R;
import com.beauty.dressing.common.bean.OrderBean;
import com.beauty.dressing.common.interfaces.OnRecyclerViewItemListener;
import com.beauty.dressing.common.utils.Constants;
import com.beauty.dressing.rxandroid.api.ApiManager;
import com.xiaolanba.commonlib.fresco.FrescoUtil;

import java.util.ArrayList;
import java.util.List;

public class MyOrderAdapter extends RecyclerView.Adapter<MyOrderAdapter.MyOrderViewHolder> {
    private Context context;
    private List<OrderBean> mList;
    private OnRecyclerViewItemListener onRecyclerViewItemListener;

    public void setData(List<OrderBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public MyOrderAdapter(Context context) {
        this.context = context;
        mList = new ArrayList<>();
    }


    @Override
    public MyOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.my_order_item, parent, false);
        return new MyOrderViewHolder(view);
    }


    @Override
    public void onBindViewHolder(MyOrderViewHolder holder, final int position) {
        holder.order_number.setText(mList.get(position).getOrder_number());
        holder.order_money.setText("￥" + mList.get(position).getOrder_price());
        holder.order_status.setText(mList.get(position).getStatus_name());
        FrescoUtil.loadImage(holder.shop_image, ApiManager.HEAR_URL + mList.get(position).getImage());
        holder.lease_name.setText(mList.get(position).getLease_name());
        holder.order_number.setText(mList.get(position).getOrder_number());
        switch (String.valueOf(mList.get(position).getOrder_status())) {
            case Constants.ALLORDER://全部订单

                break;
            case Constants.WAITINGCLAUSE://待支付
                holder.left_tv.setVisibility(View.INVISIBLE);
                holder.right_tv.setText("去支付");
                break;
            case Constants.WAITINGSHIPPED://待发货
                holder.left_tv.setVisibility(View.INVISIBLE);
                holder.right_tv.setText("取消订单");
                break;
            case Constants.WAITINGRECEIPT://待收货
                holder.left_tv.setText("追踪物流");
                holder.right_tv.setText("确认收货");
                holder.right_tv.setBackgroundResource(R.drawable.btn_bg_confirm);
                break;
            case Constants.COMPLETED://已完成
                holder.left_tv.setText("退换/售后");
                holder.right_tv.setText(" 评价 ");
                break;
        }

        holder.left_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRecyclerViewItemListener.onItemViewOnClick(R.id.left_tv, position);
            }
        });
        holder.right_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRecyclerViewItemListener.onItemViewOnClick(R.id.right_tv, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (!mList.isEmpty())
            return mList.size();
        else
            return 0;
    }

    public void setOnRecyclerViewItemListener(OnRecyclerViewItemListener onRecyclerViewItemListener) {
        this.onRecyclerViewItemListener = onRecyclerViewItemListener;
    }

    class MyOrderViewHolder extends RecyclerView.ViewHolder {
        TextView order_number;
        TextView order_money;
        TextView order_status;
        SimpleDraweeView shop_image;
        TextView lease_name;
        TextView order_quantity;
        TextView left_tv;
        TextView right_tv;


        MyOrderViewHolder(View itemView) {
            super(itemView);
            order_number = itemView.findViewById(R.id.order_number);
            order_money = itemView.findViewById(R.id.order_money);
            order_status = itemView.findViewById(R.id.order_status);
            shop_image = itemView.findViewById(R.id.shop_image);
            lease_name = itemView.findViewById(R.id.lease_name);
            order_quantity = itemView.findViewById(R.id.order_quantity);
            left_tv = itemView.findViewById(R.id.left_tv);
            right_tv = itemView.findViewById(R.id.right_tv);
        }
    }
}
